package com.example.kaogong.sys.controller;



import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.kaogong.common.exception.ParamsErrorException;
import com.example.kaogong.common.utils.Assert;
import com.example.kaogong.common.utils.RespBean;
import com.example.kaogong.sys.pojo.Course;
import com.example.kaogong.sys.pojo.dto.CourseAddDto;
import com.example.kaogong.sys.pojo.vo.CourseDetailInfoVo;
import com.example.kaogong.sys.pojo.vo.CourseInfoVo;
import com.example.kaogong.sys.service.ICourseService;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.security.Principal;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author    
 * @since 2022-03-26
 */
@RestController
@RequestMapping("/course")
@Api(tags = "课程接口")
@Slf4j
public class CourseController {

    @Autowired
    private ICourseService courseService;

    @ApiOperation(value = "当前登录学生收藏课程")
    @PostMapping(value = "/favorite/{courseId}")
    public RespBean favoriteCourse(@PathVariable Integer courseId, Principal principal) {
        Assert.notNull(courseId, new ParamsErrorException("课程编号不能为空"));
        if (null == principal)
            return RespBean.error();
        log.info("courseId:{}", courseId);
        RespBean respBean = courseService.favoriteCourse(courseId, principal.getName());
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "当前登录教师新增课程")
    @PostMapping("")
    public RespBean addCourse(@Valid @RequestBody CourseAddDto courseAddDto, Principal principal) {
        if (null == principal)
            return RespBean.error();
        log.info("request:{}", new Gson().toJson(courseAddDto));
        RespBean respBean = courseService.addCourse(principal.getName(), courseAddDto);
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "根据课程编号修改课程缩略图")
    @PutMapping(value = "/img/{courseId}/{fileName}")
    public RespBean updateCourseImg(@PathVariable Integer courseId, @PathVariable String fileName) {
        Assert.notNull(courseId, new ParamsErrorException("课程编号不能为空"));
        Assert.notBlank(fileName, new ParamsErrorException("文件名不能为空"));
        log.info("courseId:{}, fileName:{}", courseId, fileName);
        RespBean respBean = courseService.updateCourseImg(courseId, fileName);
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "根据课程编号修改课程基本信息")
    @PutMapping("/{courseId}")
    public RespBean updateCourseInfo(@RequestBody Course  course, @PathVariable Integer courseId) {
        Assert.notNull(courseId, new ParamsErrorException("课程编号不能为空"));
        log.info("courseId:{}, request:{}", courseId, new Gson().toJson(course));
        RespBean respBean = courseService.updateCourseInfo(course, courseId);
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "根据课程编号查询课程基本信息")
    @GetMapping("/{courseId}")
    public RespBean<CourseInfoVo> getCourseInfoById(@PathVariable Integer courseId) {
        Assert.notNull(courseId, new ParamsErrorException("课程编号不能为空"));
        log.info("courseId:{}", courseId);
        RespBean<CourseInfoVo> respBean = courseService.getCourseInfoById(courseId);
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "分页获取当前教师上传课程的基本信息", notes = "教师课程管理显示信息")
    @GetMapping(value = "/teacher/{current}/{size}")
    public RespBean<Page<CourseInfoVo>> getCurrentTeacherCourse(@PathVariable Long current, @PathVariable Long size, Principal principal) {
        if (null == principal)
            return RespBean.error();
        Assert.notNull(current, new ParamsErrorException("current 不能为空"));
        Assert.notNull(size, new ParamsErrorException("size 不能为空"));
        RespBean<Page<CourseInfoVo>> respBean = courseService.getCourseInfo(current, size, principal.getName());
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "根据课程编号获取课程详细信息")
    @GetMapping(value = "/detail/{courseId}")
    public RespBean<CourseDetailInfoVo> getCourseDetailInfoByCourseId(@PathVariable Integer courseId) {
        Assert.notNull(courseId, new ParamsErrorException("课程编号不能为空"));
        log.info("courseId:{}", courseId);
        RespBean<CourseDetailInfoVo> respBean = courseService.getCourseDetailInfoByCourseId(courseId);
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "根据课程编号删除课程")
    @DeleteMapping("/{courseId}")
    public RespBean deleteCourseByCourseId(@PathVariable Integer courseId) {
        Assert.notNull(courseId, new ParamsErrorException("课程编号不能为空"));
        log.info("courseId:{}", courseId);
        RespBean respBean = courseService.deleteCourseByCourseId(courseId);
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "分页获取所有课程基本信息", notes = "网站首页展示信息， 无需token验证")
    @GetMapping(value = "/all/{current}/{size}")
    public RespBean<Page<CourseInfoVo>> getCourseBasicInfo(@PathVariable Long current, @PathVariable Long size) {
        Assert.notNull(current, new ParamsErrorException("current 不能为空"));
        Assert.notNull(size, new ParamsErrorException("size 不能为空"));
        RespBean<Page<CourseInfoVo>> respBean = courseService.getCourseInfo(current, size, null);
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }
}
